﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.OleDb;

namespace TempDataInterfaceDLL
{
    [Serializable]
    public class OldTempData
    {

        public const string CALLERField = "@Caller";

        public const string CALLINTIMEField = "@CallInTime";

        public const string ERRFLAGField = "@ErrFlag";

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string TEMPERATUREField = "@Temperature";

        private String _Caller;

        private DateTime _CallInTime;

        private Byte _ErrFlag;

        private Int32 _ID;

        private Single _Temperature;

        public String Caller
        {
            get { return _Caller; }
            set { _Caller = value; }
        }
        public DateTime CallInTime
        {
            get { return _CallInTime; }
            set { _CallInTime = value; }
        }
        public Byte ErrFlag
        {
            get { return _ErrFlag; }
            set { _ErrFlag = value; }
        }
        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public Single Temperature
        {
            get { return _Temperature; }
            set { _Temperature = value; }
        }
        public OldTempData()
        {
            _Caller = "";
            _CallInTime = DateTime.Now;
            _ErrFlag = 0;
            _ID = 0;
            _Temperature = 0;
        }
    }

    public class OldTempDataFactory
    {
        public static OldTempData Construct(IDataReader reader)
        {
            OldTempData data = new OldTempData();

            try { data.Caller = Convert.ToString(reader["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(reader["CallInTime"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(reader["ErrFlag"]); }
            catch { }

            try { data.ID = Convert.ToInt32(reader["ID"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(reader["Temperature"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Int32 GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dt.Rows.Count < rowIndex + 1)

                return 0;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return 0;

            try { PrimaryKey = Convert.ToInt32(dr[OldTempData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static OldTempData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static OldTempData Construct(DataTable dt, int rowIndex)
        {
            OldTempData data = new OldTempData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.Caller = Convert.ToString(dr["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(dr["CallInTime"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(dr["ErrFlag"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(dr["Temperature"]); }
            catch { }

            return data;
        }
        public static OldTempData Construct(DataGridView dgv, int rowIndex)
        {
            OldTempData data = new OldTempData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.Caller = Convert.ToString(dr["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(dr["CallInTime"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(dr["ErrFlag"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(dr["Temperature"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dgv.Rows.Count < rowIndex + 1)

                return 0;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return 0;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == OldTempData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToInt32(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static OldTempData Construct(DataRow dr)
        {
            OldTempData data = new OldTempData();

            try { data.Caller = Convert.ToString(dr["Caller"]); }
            catch { }

            try { data.CallInTime = Convert.ToDateTime(dr["CallInTime"]); }
            catch { }

            try { data.ErrFlag = Convert.ToByte(dr["ErrFlag"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.Temperature = Convert.ToSingle(dr["Temperature"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataRow dr)
        {
            Int32 PrimaryKey = 0;

            try { PrimaryKey = Convert.ToInt32(dr[OldTempData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class OldTempDataDBOption
    {
        public static int Insert(OldTempData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into OldTemp (");
            sql.Append("Caller, ");
            sql.Append("CallInTime, ");
            sql.Append("ErrFlag, ");
            sql.Append("Temperature");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.Caller.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("#");
            sql.Append(d.CallInTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("#");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.ErrFlag.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.Temperature.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(OldTempData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update OldTemp set ");
            sql.Append("Caller=");
            sql.Append("'");
            sql.Append(d.Caller.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("CallInTime=");
            sql.Append("#");
            sql.Append(d.CallInTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("#");
            sql.Append(", ");
            sql.Append("ErrFlag=");
            sql.Append("");
            sql.Append(d.ErrFlag.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("Temperature=");
            sql.Append("");
            sql.Append(d.Temperature.ToString());
            sql.Append("");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(OldTempData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from OldTemp ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from OldTemp ");
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(OldTempData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from OldTemp where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from OldTemp ");
            return WFNetLib.ADO.AccessOP.DataSetSQL(sql.ToString());
        }
        public static OleDbDataReader ReaderSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from OldTemp ");
            return WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql.ToString());
        }
        public static OldTempData GetOldTemp(string caller)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from OldTemp where Caller='"+caller+"'");
            DataTable dt= WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
            {
                return OldTempDataFactory.Construct(dt.Rows[0]);
            }
        }
        public static OldTempData GetLastOldTemp()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select TOP 1 * from OldTemp  Order By CallInTime Desc");
            DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
            {
                return OldTempDataFactory.Construct(dt.Rows[0]);
            }
        }
        public static void SavwOldTemp(OldTempData oldTd)
        {
            OldTempData oldTd1 = GetOldTemp(oldTd.Caller);
            if (oldTd1 == null)//之前不存在
            {
                Insert(oldTd);
            }
            else
            {
                oldTd.ID = oldTd1.ID;
                Update(oldTd);
            }
        }
    }
}